var date = echartUtil.GetDateStr(0);
var month = date.substring(0, 7);
$(function () {
	page.bindTag();
	nameToIdUtil.getProductionDepartment("productionDepartment");
	$("#month").val(month);
});

function dataChange() {
	month = $("#month").val();
}

var page = { //页面总变量
	//绑定页面元素
	bindTag: function () {

		//点击查询按钮时触发方法
		$("#queryCapacity").bind("click", function () {
			var deptId = $("#productionDepartment option:selected").val();
			var workShopId = $("#workshopName option:selected").val();
			page.getCapacity(deptId, workShopId, month);
		});


		$("#downlodeExcle").bind("click", function () {
			//				var url="workshop/downlodeCapaCity?workshopName="+workshopName+"&workingProcedureName="+workingProcedureName+"&listString="+titleList+"&startDay="+startDay+"&date="+date;
			tableUtil.download(url);
		});

		$("#productionDepartment").bind("change", function () {
			var deptId = $("#productionDepartment").val();
			page.getWorkshopName(deptId);
		});

		$("#otherModel").bind("click", function () {
			//				$('#queryCapacityTable').dataTable().fnDestroy();
			tableUtil.dataTablesStaticUtil("queryCapacityTable", 8, true);
			$("#otherModel").addClass("hidden");
		});

		$("#exportExcel").on('click', function () {
			var $table = $('#queryCapacityTable');
			export_table_to_excel($table[0], '产能汇总', {
				sheetName: 'sheet1'
			});
		});
	},
	//获取车间名称
	getWorkshopName: function (deptId) {
		ajaxUtil.ajaxRuquest('basicData/getDeptNoMegerWorkShop', {
			"deptId": deptId
		}, function (data) {
			var html = "";
			for (var i = 0; i < data.length; i++) {
				html = html + "<option value='" + data[i].id + "'>" + data[i].workshopName + "</option>";
			}
			$("#workshopName").html("<option value=''>请选择</option>");
			$("#workshopName").append(html);
		});
	},
	//根据车间与工序名称获取该车间所有工序产能信息（thArray为工序数组，决定表格头）
	getCapacity: function (deptId, workShopId, month) {
		$('#queryCapacityTable').dataTable().fnDestroy();
		$("#queryCapacityTable").html("");
		ajaxUtil.ajaxRuquest('workshop/queryCapacity', {
			"deptId": deptId,
			"workShopId": workShopId,
			"month": month
		}, function (data) {
			var dayTitle = page.getTitle(month, 1);
			var monthDayList = page.getTitle(month, 2);
			var listCapacity = data;
			//表头
			var html = "<thead><tr><th>车间</th><th>合计</th><th>计划日<br/>产能</th><th>月计划<br/>产能</th><th>排单<br/>产能</th><th>每日实<br/>际产能</th><th>产能达<br/>成率</th><th>车间周<br/>转量</th>";
			for (var i = dayTitle.length - 1; i >= 0; i--) {
				html += "<th>" + dayTitle[i].substring(8, 10) + "</th>";
			}
			html += "</tr></thead>";
			html = $(html);
			html.data('dayTitle', dayTitle);
			$("#queryCapacityTable").append(html);
			//表体
			for (var i = 0; i < listCapacity.length; i++) {
				var sumCapacity = 0; //车间产能和
				var listDuty = listCapacity[i].listDuty; //车间产能明细
				var dayCapacity = listCapacity[i].dayCapacity; //车间日产能
				//月计划产能   : 日产能    * 月工作天数
				var monthPlanCapacity = dayCapacity * page.getMonthWorkDay(listCapacity[i].restType, monthDayList);

				//获取产能合计
				for (var x = 0; x < listDuty.length; x++) { //车间产能之后等于每天产能相加
					sumCapacity = sumCapacity + listDuty[x].capacity;
				}

				//实际上班天数
				var aleradyWorkDay = page.getMonthWorkDay(listCapacity[i].restType, dayTitle);

				//每日实际产能
				if (aleradyWorkDay == 0) {
					aleradyWorkDay = 1;
				}
				var dayReachCapacity = sumCapacity / aleradyWorkDay;
				dayReachCapacity = dayReachCapacity.toFixed(2);



				//获取达成率
				var daChengLv = 0;
				if (dayCapacity * aleradyWorkDay != 0) {
					daChengLv = sumCapacity / (dayCapacity * aleradyWorkDay) * 100;
				} else {
					daChengLv = 0.00;
				}
				daChengLv = daChengLv.toFixed(2);

				//如果日平均产能大于车间日产能底色为绿色  否则为#FFE4B5
				if (sumCapacity / dayTitle.length >= dayCapacity) {
					html = "<tr style='background-color:green;'>";
				} else {
					html = "<tr style='background-color:#FFE4B5;'>";
				}

				html += "<th>" + listCapacity[i].workShopName + "</th><td><a class='sumCapacity'>" + sumCapacity + "</a></td><td>" + dayCapacity + "</td><td>" + monthPlanCapacity + "</td><td>" + listCapacity[i].turnover + "</td><td>" + dayReachCapacity + "</td><td>" + daChengLv + "%</td><td>" + listCapacity[i].workShopStartTurnover + "</td>";
				//循环表头
				for (var j = dayTitle.length - 1; j >= 0; j--) {
					var capacity = "0";
					for (var z = 0; z < listDuty.length; z++) {
						//如果此日期有产能，返回产能
						if (listDuty[z].delayTime == dayTitle[j]) {
							capacity = listDuty[z].capacity;
							break;
						}
					}
					//如果当日产能小于日产能  红色显示
					if (capacity >= dayCapacity) {
						html += "<td><a class='capacity'>" + capacity + "</a></td>";
					} else {
						html += "<td><a class='capacity' style='color:red;'>" + capacity + "</a></td>";
					}
				}
				html += "</tr>";
				html = $(html);
				html.data('data', listCapacity[i]);
				$("#queryCapacityTable").append(html);
			}
			//				$("#queryCapacityTable").html(html);
			//				tableUtil.dataTablesStaticUtil("queryCapacityTable",8);
			page.pageBind();
			$("#queryMonth").html(month);
			$("#otherModel").removeClass("hidden");
		});
	},
	pageBind: function () {
		$(".capacity").unbind();
		$(".sumCapacity").unbind();


		/**
		 * 点击日产能查看明细
		 */
		$(".capacity").bind("click", function () {
			var capacity = $(this).text();
			if (capacity === '0') {
				layAlert("没有报工明细");
				return false;
			}
			var trData = $(this).parents("tr").data("data");
			var index = $(this).parents("td").index() - 7;


			var dayTitle = $("#queryCapacityTable thead").data("dayTitle");
			var day = dayTitle[dayTitle.length - index];
			var workShopId = trData.workShopId;
			ajaxUtil.ajaxRuquestNoQuan('workshop/dayCapacityDetail', {
				'day': day,
				'workShopId': workShopId
			}, function (data) {
				page.setDetailView(data);
				$('#dayCapacityDeatil').dataTable();
			});

		});


		/**
		 * 点击合计查看明细
		 */
		$(".sumCapacity").bind("click", function () {
			var capacity = $(this).text();
			if (capacity === '0') {
				layAlert("没有报工明细");
				return false;
			}
			var trData = $(this).parents("tr").data("data");
			var month = $("#queryMonth").text();
			var workShopId = trData.workShopId;
			ajaxUtil.ajaxRuquestNoQuan('workshop/dayCapacityDetail', {
				'month': month,
				'workShopId': workShopId
			}, function (data) {
				page.setDetailView(data);

			});
		});

	},
	setDetailView: function (data) {
		if (data.length <= 0) {
			layAlert("没有报工明细");
			return false;
		}
		$('#dayCapacityDeatil').dataTable().fnDestroy();
		$("#dayCapacityDeatilTbody").html("");
		var html = "";
		for (var i = 0; i < data.length; i++) {
			var  time=data[i].delayTime;
			time=time.substring(5,16);
			html += "<tr>" +
				"<td>" + data[i].companyOrderId + "</td>" +
				"<td>" + data[i].factoryId + "</td>" +
				"<td>" + data[i].customerId + "</td>" +
				"<td>" + data[i].maching + "</td>" +
				"<td>" + data[i].color + "</td>" +
				"<td>" + data[i].model + "</td>" +
				"<td>" + data[i].orderCount + "</td>" +
				"<td>" + data[i].qualifiedCount + "</td>" +
				"<td>" + data[i].userName + "</td>" +
				"<td>" + time + "</td>" +
				"</tr>";
		}
		$("#dayCapacityDeatilTbody").append(html);
		tableUtil.dataTablesStaticUtil("dayCapacityDeatil", 0, false);
		$("#setRoleModal").modal("show");
	},
	/**
	 * 获取日期表头
	 * month 月份
	 * type 类型 1 查询当前月到目前为止天数  2 查询当前月总天数
	 */
	getTitle: function (month, type) {
		var array = new Array();
		var thisMonth = date.substring(0, 7); //本月分
		var lastDay = 30;
		if (thisMonth == month && type == 1) {
			lastDay = moment(date).date(); //如果是本月分，最后一天是今天
		} else {
			lastDay = moment(month).endOf("month").date(); //否则是当月最后一天
		}
		for (var i = 1; i <= lastDay; i++) {
			var day = "-0";
			if (i < 10) {
				day += i;
			} else {
				day = "-" + i;
			}
			array.push(month + day);
		}
		return array;
	},
	/**
	 * restType 休息类型
	 * listDay 查询日期集合
	 */
	getMonthWorkDay: function (restType, listDay) {
		var workDay = 0; //当月实际工作天数：当月天数减去当月休息天数
		switch (restType) {
			case 0:
				workDay = listDay.length;
				break; //月休0填
			case 1: //月休一天
				var noWorkDay = 0;
				var day = Number(listDay[listDay.length - 1].substring(8, 10));
				if (day > 1) {
					noWorkDay = noWorkDay + 1;
				}
				workDay = listDay.length - noWorkDay;
				break;
			case 2: //月休两天
				var noWorkDay = 0;
				var day = Number(listDay[listDay.length - 1].substring(8, 10));
				if (day > 15) {
					noWorkDay = noWorkDay + 2;
				} else if (day > 1) {
					noWorkDay = noWorkDay + 1;
				}
				workDay = listDay.length - noWorkDay;
				break;
			case 3: //月休三天
				var noWorkDay = 0;
				var day = Number(listDay[listDay.length - 1].substring(8, 10));
				if (day > 20) {
					noWorkDay = noWorkDay + 3;
				} else if (day > 10) {
					noWorkDay = noWorkDay + 2;
				} else if (day > 1) {
					noWorkDay = noWorkDay + 1;
				}
				workDay = listDay.length - noWorkDay;
				break;
			case 4: //周休一天
				var noWorkDay = 0;
				for (var i = 0; i < listDay.length; i++) {
					if (moment(listDay[i]).day() == 0) { //如果是星期天,休息天加1
						noWorkDay = noWorkDay + 1;
					}
				}
				workDay = listDay.length - noWorkDay;
				break;
			case 5: //周休1.5天
				var noWorkDay = 0;
				for (var i = 0; i < listDay.length; i++) {
					if (moment(listDay[i]).day() == 0) { //如果是星期天,休息天加1
						noWorkDay = noWorkDay + 1;
					}
					if (moment(listDay[i]).day() == 6) { //如果是星期6,休息天加0.5
						noWorkDay = noWorkDay + 0.5;
					}
				}
				workDay = listDay.length - noWorkDay;
				break;
			case 6: //周休2天
				var noWorkDay = 0;
				for (var i = 0; i < listDay.length; i++) {
					if (moment(listDay.length[i]).day() == 0 || moment(listDay.length[i]).day() == 6) { //如果是星期天，星期六,休息天加1
						noWorkDay = noWorkDay + 1;
					}
				}
				workDay = listDay.length - noWorkDay;
				break;
		}
		return workDay;
	}
}